package com.earl.gsm.web.match;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.gsm.match.ApplyMatch;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.ApplyMatchService;
import com.earl.gsm.service.UserService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/match/apply")
//@Api(value="Person Rest Service") //修改生成的son路径
public class ApplyMatchController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(ApplyMatchController.class);

    @Autowired
    private ApplyMatchService service;
    @Autowired
    private UserService userService;

    /**
     * GET /users -> get all the users
     */
    @RequestMapping(value = "/all")
    @ApiOperation(value = "得到所有的申请信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView getAll() {
        log.debug("page show all apply for create match");
        return new ModelAndView("match/allApply","applyList",service.findAll());
    }

    @RequestMapping(value = "/undeal")
    @ApiOperation(value = "未处理的申请", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView unDealApply() {
        log.debug("page show all apply for create match");
        ApplyMatch applyMatch = new ApplyMatch();
        applyMatch.setPass(false);
        return new ModelAndView("match/allApply","applyList",service.searchQuery(applyMatch));
    }

    @RequestMapping(value = "/havedeal")
    @ApiOperation(value = "已经处理的申请", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView haveDealApply() {
        log.debug("page show all apply for create match");
        ApplyMatch applyMatch = new ApplyMatch();
        applyMatch.setPass(true);
        return new ModelAndView("match/allApply","applyList",service.searchQuery(applyMatch));
    }

    
    @RequestMapping(value = "/preAdd")
    @ApiOperation(value = "已经处理的申请", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public String preAdd() {
        log.debug("page show all apply for create match");
        ApplyMatch applyMatch = new ApplyMatch();
        applyMatch.setPass(true);
        return "match/addApply";
    }
    
    @RequestMapping(value = "/add")
    @ApiOperation(value = "已经处理的申请", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public DwzMessage add(ApplyMatch apply) {
        log.debug("page show all apply for create match");
        apply.setPass(false);
        apply.setUserId(1L);
       service.save(apply);
        return new DwzMessage(200,"kxoxjsf");
    }

    /**
     * 服务端添加赛事
     * @param apply
     * @return
     */
    @RequestMapping(value="/pass/{applyId}")
    @ApiOperation(value = "添加申请", notes = "add new match", httpMethod = "GET", response = User.class, responseContainer = "List")
    public DwzMessage applyForMatch(@PathVariable Long applyId){
        ApplyMatch applyMatch = service.findOne(applyId);
        applyMatch.setPass(true);
        
        User user = userService.findOne(applyMatch.getUserId());
        user.setCreateMatch(true);
        
        service.updateByPrimaryKeySelective(applyMatch);
        userService.updateByPrimaryKeySelective(user);
        
        
        DwzMessage dwzMessage = new DwzMessage(200,"添加成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/apply/all");
        return dwzMessage;
    }

    /**
     * 服务端删除赛事
     * @param id applyid
     * @return
     */
    @RequestMapping(value="/delete/{id}")
    @ApiOperation(value = "删除赛事", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id){
        service.delete(id);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setMessage("删除成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/match/apply/all");
        dwzMessage.setStatusCode(200);
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }
}
